package com.service.impl;

import com.bean.HwStu;
import com.dao.HwStuAttachDao;
import com.dao.HwStuDao;
import com.dao.impl.HwStuAttachDaoImpl;
import com.dao.impl.HwStuDaoImpl;
import com.service.HwStuAttachService;
import com.service.HwStuService;
import com.utils.Page;

import java.util.List;

public class HwStuServiceImpl implements HwStuService {
    HwStuDao hwStuDao = new HwStuDaoImpl();
    HwStuAttachDao hwStuAttachDao=new HwStuAttachDaoImpl();
    @Override
    public Page<HwStu> page(int pageNo, int pageSize, String content) {
        Page<HwStu> page = new Page<>();
        page.setPageSize(pageSize);
        Integer pageTotalCount = hwStuDao.selectForPageTotalCount(content);
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        int begin = (page.getPageNo() - 1) * pageSize;
        List<HwStu> items = hwStuDao.selectForPageItems(begin, pageSize, content);
        // 设置当前页数据
        page.setItems(items);
        return page;
    }

    @Override
    public HwStu selectBy(HwStu record) {
        return hwStuDao.selectBy(record);
    }

    @Override
    public int deleteById(Integer id) {
        int status = 0;
        hwStuAttachDao.deleteByHwStuId(id);
        if (hwStuDao.deleteByPrimaryKey(id) == -1) {
            status = 1;
        }
        return status;
    }

    @Override
    public int updeteInfo(HwStu record) {
        int status = 0;
        if (hwStuDao.updateByPrimaryKey(record) == -1) {
            status = 1;
        }
        return status;
    }

    @Override
    public int insert(HwStu record) {
        int status = 0;
        if (hwStuDao.insert(record) == -1) {
            status = 1;
        }
        return status;
    }

    @Override
    public Page<HwStu> approved(Integer id, int pageNo, int pageSize) {
        if (id == null) {
            return null;
        }
        Page<HwStu> page = new Page<>();
        page.setPageSize(pageSize);
        Integer pageTotalCount = hwStuDao.approvedCount(id);
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        int begin = (page.getPageNo() - 1) * pageSize;
        List<HwStu> items = hwStuDao.approved(id, begin, pageSize);
        // 设置当前页数据
        page.setItems(items);
        return page;
    }
}
